# Contributor: Fabricio Silva <hi@fabricio.dev>
# Maintainer: Fabricio Silva <hi@fabricio.dev>
pkgname=lidarr
pkgver=2.2.5.4141
pkgrel=0
pkgdesc="Music download automation for usenet and torrents."
url="https://github.com/Lidarr/Lidarr"
arch="x86_64 aarch64 armv7"
license="GPL-3.0-only"
options="net !check" # no tests
depends="
	aspnetcore6-runtime
	chromaprint
	sqlite-libs
	"
makedepends="
	dotnet6-sdk
	yarn
	"
subpackages="$pkgname-openrc"
install="$pkgname.pre-install"
source="
	$pkgname-$pkgver.tar.gz::https://github.com/Lidarr/Lidarr/archive/refs/tags/v$pkgver.tar.gz
	0001-disable-restart.patch
	lidarr.initd
	lidarr.confd
	package_info
	"
builddir="$srcdir/Lidarr-$pkgver"
pkgusers="lidarr"
pkggroups="lidarr"

# map arch to dotnet
case $CARCH in
	x86_64) _dotnet_arch="x64" ;;
	aarch64) _dotnet_arch="arm64" ;;
	armv7) _dotnet_arch="arm" ;;
	*) _dotnet_arch="$CARCH" ;;
esac

# custom variables
_runtime="linux-musl-$_dotnet_arch"
_framework="net6.0"
_output="_output"
_artifacts="$_output/$_framework/$_runtime/publish"

prepare() {
	default_prepare

	# increase max opened files
	ulimit -n 4096

	# replace version info
	local buildprops=src/Directory.Build.props
	sed -i "/<AssemblyVersion>/s/>.*<\//>$pkgver<\//" "$buildprops"
	sed -i "/<AssemblyConfiguration>/s/>.*<\//>master<\//" "$buildprops"

	export BROWSERSLIST_IGNORE_OLD_DATA=true
	yarn install --frozen-lockfile --network-timeout 120000
}

build() {
	dotnet build src \
		-p:RuntimeIdentifiers="$_runtime" \
		-p:Configuration=Release \
		-p:SelfContained=false \
		-t:PublishAllRids
	yarn build --env production --no-stats

	# use fpcalc from chromaprint package
	rm -f "$_artifacts"/fpcalc

	# cleanup
	find "$_artifacts" \( \
		-name "ServiceUninstall.*" -o \
		-name "ServiceInstall.*" -o \
		-name "Lidarr.Windows.*" \) -delete

	mv "$_output"/UI "$_artifacts"
}

package() {
	local destdir="$pkgdir"/usr/lib/lidarr

	# use package_info to disable update feature
	install -Dm644 "$srcdir"/package_info "$destdir"/package_info
	echo "PackageVersion=$pkgver-r$pkgrel" >>"$destdir"/package_info

	cp -af "$_artifacts" "$destdir"/bin
	chown -R "$pkgusers:$pkggroups" "$destdir"

	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -Dm644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
}

sha512sums="
6bda1ea54ae0fc78e5dc2b4b944e335e3f76e8c2b4756f9875a2d931466a9de381c6276b50117ad79c7d70442af410a4cbaefb2878f47caa335dfee09864aeb6  lidarr-2.2.5.4141.tar.gz
753520379a6c5f2899b4ddfd820d5573598a32cb105f74c6fd978a3f2c98895cf709ef2d9afe2bae8c4856da86cb6a1364f5713ea7ef6a36bb21b5a5840c1471  0001-disable-restart.patch
194dce13e5220d71ffe262d40dbd1b124166f158500a420a8032279baa8f046c1772857cb7f4ab9428384481b962ff4939de0e2cdc6197f91cd35d1c0871a8e4  lidarr.initd
babbb145648ad296b53271db5c642edddb9d0a5173479c4e383c48aab1d6b616c176e1ef73ae78078f41b4e6ca0e5768a96b36d844a3669f59179226ba8e97ad  lidarr.confd
67c4dd1cf8259296d994c350dfd3962d23333071889ce0b4ef6b2f4cbb4349490c5dbe4dcc202c99bab7a5c4b7611be56d7c8835b2b13924526c45311db1c3fb  package_info
"
